<?php

class Site extends Controller {
	
	function Site()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->database();
		$this->load->library('parser');
		$this->load->helper('file');
		
		if($this->uri->segment(1)){
			$page = $this->uri->segment(1);
		} else {
			$page = 'home';
		}
		
		//get theme
		$query = $this->db->query("SELECT * FROM gcms_options WHERE option_name='theme'");
		$row = $query->row();
		$theme = $row->option_value;
		//get title
		$query = $this->db->query("SELECT * FROM gcms_options WHERE option_name='title'");
		$row = $query->row();
		$title = $row->option_value;
		//get content
		$query = $this->db->query("SELECT * FROM gcms_posts WHERE post_slug='$page'");
		if ($query->num_rows() > 0){ //If this is a page
			$row = $query->row();
			$content = $row->post_content;
			$page_title = $row->post_title;
		} else {
			$content = "<h1>Error 404</h1>This page does not exist";
			$page_title = "Error: 404";
		}
		//get pages
		$nav = '<div class="navigation"><ul>';
		$query = $this->db->query("SELECT * FROM gcms_posts ORDER BY post_order ASC");
		foreach ($query->result() as $row)
		{
			if($row->post_menu == 1){ //if its meant to be in the menu
				if($row->post_slug == $page || $row->post_slug == ''){ //active page
					$nav .= '<li><a href="'.base_url().$row->post_slug.'" class="active">'.$row->post_title.'</a></li>';
				} else {
					$nav .= '<li><a href="'.base_url().$row->post_slug.'">'.$row->post_title.'</a></li>';
				}
			}
		}
		$nav .= '</ul></div>';
		//get news
		$news = '';
		$i = 0;
		$query = $this->db->query("SELECT * FROM gcms_news ORDER BY news_date DESC");
		foreach ($query->result() as $row)
		{
			if($i < 6){ //only show 6
				$news .= '<h2>'.$row->news_heading.'</h2><p>'.$row->news_content.'</p>';
			}
			$i++;
		}

		$data = array(
			'theme' => $theme,
			'title' => $title,
            'page' => $page_title,
            'content' => $content,
			'list_pages' => $nav,
			'news' => $news,
			'base_url' => base_url(),
			'base_theme' => base_url().'themes/'.$theme.'/'
            );

		$this->parser->parse("../../../themes/$theme/index", $data);
	}	
}
?>